package mao.leetcode.dp.leetcode53;

public class Solution {

    public static void main(String[] args) {
        int[] a={-4,-3,-2};
        maxProduct_1(a);
    }
    public static int maxProduct_1(int[] nums) {
        int ans=nums[0];
        int cur_max=nums[0];
        int cur_min=nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (nums[i]>=0) {
                cur_max = Math.max(cur_max*nums[i],nums[i]);
                cur_min = Math.min(cur_min*nums[i],nums[i]);
            }else {
                int temp=cur_max;
                cur_max = Math.max(cur_min*nums[i],nums[i]);
                cur_min = Math.min(temp*nums[i],nums[i]);
            }
            ans= Math.max(cur_max,ans);
        }
        return ans;
    }

    public static int maxProduct(int[] nums) {
        int ans=nums[nums.length-1];
        int cur_max=nums[nums.length-1];
        int cur_min=nums[nums.length-1];
        for (int i = nums.length-2; i>=0; i--) {
            if (nums[i]<0){
                int temp = cur_max;
                cur_max=cur_min;
                cur_min=temp;
            }
            cur_max = Math.max(cur_max*nums[i],nums[i]);
            cur_min = Math.min(cur_min*nums[i],nums[i]);
            ans= Math.max(ans,cur_max);
        }
        return ans;
    }
}
